package com.androidhelper.module.common.http.control;

import com.androidhelper.module.common.bean.BaseResponse;
import com.androidhelper.module.common.http.exception.ExceptionHandle;
import com.androidhelper.module.common.http.common.ErrorCode;

import io.reactivex.observers.DisposableObserver;


//public abstract class BaseObserver<T> implements Observer<BaseResponse<T>> {
public abstract class BaseObserver<T> extends DisposableObserver<BaseResponse<T>> {



    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void onNext(BaseResponse<T> tBaseResponse) {
                 if(tBaseResponse.getErrorCode()== ErrorCode.SUCCESS){
                    if(tBaseResponse.getData()!=null){onSuccess(tBaseResponse.getData());}
                 }//else if(tBaseResponse.getErrorCode() == ErrorCode.TOKEN_INVALID){
                     // TODO 处理 token 过期
               //  }
                 else{
                     onFailure(new Exception(tBaseResponse.getErrorMsg()), tBaseResponse.getErrorMsg());
                 }
    }




    @Override
    public void onComplete() {

    }


    @Override
    public void onError(Throwable e) {
        onFailure(e, ExceptionHandle.handleException(e));
    }


    public abstract void onSuccess(T data);

    public abstract void onFailure(Throwable e, String errorMsg);


}
























